package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.PcSilverConfig;
import com.ruoyi.db.mapper.system.MpClientMapper;
import com.ruoyi.db.mapper.system.PcSilverConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IPcSilverConfigService;

import javax.transaction.Transactional;

/**
 * 银藻系统配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-27
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class PcSilverConfigServiceImpl implements IPcSilverConfigService 
{
    @Autowired
    private PcSilverConfigMapper pcSilverConfigMapper;
    @Autowired
    private MpClientMapper mpClientMapper;

    /**
     * 查询银藻系统配置
     * 
     * @param id 银藻系统配置主键
     * @return 银藻系统配置
     */
    @Override
    public PcSilverConfig selectPcSilverConfigById(Long id)
    {
        return pcSilverConfigMapper.selectPcSilverConfigById(id);
    }

    /**
     * 查询银藻系统配置列表
     * 
     * @param pcSilverConfig 银藻系统配置
     * @return 银藻系统配置
     */
    @Override
    public List<PcSilverConfig> selectPcSilverConfigList(PcSilverConfig pcSilverConfig)
    {
        List<PcSilverConfig> list = pcSilverConfigMapper.selectPcSilverConfigList(pcSilverConfig);
        for (PcSilverConfig config: list) {
            config.setTransferRate(config.getTransferRate().multiply(new BigDecimal(100)));
            config.setCashReplaceRate(config.getCashReplaceRate().multiply(new BigDecimal(100)));
        }
        return list;
    }

    /**
     * 新增银藻系统配置
     * 
     * @param pcSilverConfig 银藻系统配置
     * @return 结果
     */
    @Override
    public int insertPcSilverConfig(PcSilverConfig pcSilverConfig)
    {
        return pcSilverConfigMapper.insertPcSilverConfig(pcSilverConfig);
    }

    /**
     * 修改银藻系统配置
     * 
     * @param pcSilverConfig 银藻系统配置
     * @return 结果
     */
    @Override
    public int updatePcSilverConfig(PcSilverConfig pcSilverConfig)
    {
        pcSilverConfig.setTransferRate(pcSilverConfig.getTransferRate().divide(new BigDecimal(100)));
        pcSilverConfig.setCashReplaceRate(pcSilverConfig.getCashReplaceRate().divide(new BigDecimal(100)));
        List<MpClient> clients = mpClientMapper.selectMpClientList(new MpClient());
        for (MpClient client:clients) {
            MpClient client1 = new MpClient();
            client1.setClientId(client.getClientId());
            client1.setSilverTransferLimit(pcSilverConfig.getTransferLimit());
            mpClientMapper.updateMpClient(client1);
        }
        return pcSilverConfigMapper.updatePcSilverConfig(pcSilverConfig);
    }

    /**
     * 批量删除银藻系统配置
     * 
     * @param ids 需要删除的银藻系统配置主键
     * @return 结果
     */
    @Override
    public int deletePcSilverConfigByIds(Long[] ids)
    {
        return pcSilverConfigMapper.deletePcSilverConfigByIds(ids);
    }

    /**
     * 删除银藻系统配置信息
     * 
     * @param id 银藻系统配置主键
     * @return 结果
     */
    @Override
    public int deletePcSilverConfigById(Long id)
    {
        return pcSilverConfigMapper.deletePcSilverConfigById(id);
    }
}
